<?php
class StaffsController extends AppController {

	var $name = 'Staffs';
	var $components = array('Auth');		
	
	function beforeFilter() {		
		parent::beforeFilter();
		$this->layout="admin";		
		$this->Auth->authorize = 'controller';		
	}

    function isAuthorized() {
		$MY_RULE_ID = 7;
        if ($this->action == 'login' || $this->action == 'logout') {            
            return true;
        }else{
			$allow = $this->requestAction('ruleStaffTypes/allow/'.$MY_RULE_ID);
			if ($allow){
				return true;
			}else{
				$this->Session->setFlash(__('Permission denied!',true));
				$this->redirect(array("controller"=>"staffs","action"=>"login","admin"=>false));
				return false;
			}
        }
        return false;
    }
	
	function index() {
		/*$this->Staff->recursive = 0;
		$this->set('staffs', $this->paginate());		*/
		$this->redirect(array('action' => 'index','admin'=>true));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid staff', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('staff', $this->Staff->read(null, $id));		
	}

	function login()
	{
		if ($this->Auth->user()) {			
        }		
		$this->set('password',$this->Auth->password("admin"));		
	}
	function admin_login()
	{		
		$this->redirect(array('action' => 'login','admin'=>false));
	}
	function logout() {
		$this->redirect($this->Auth->logout());
	}
	function add() {
		if (!empty($this->data)) {
			$this->Staff->create();
			if ($this->Staff->save($this->data)) {
				$this->Session->setFlash(__('The staff has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The staff could not be saved. Please, try again.', true));
			}
		}
		$staffTypes = $this->Staff->StaffType->find('list');
		$this->set(compact('staffTypes'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid staff', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Staff->save($this->data)) {
				$this->Session->setFlash(__('The staff has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The staff could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Staff->read(null, $id);
		}
		$staffTypes = $this->Staff->StaffType->find('list');
		$this->set(compact('staffTypes'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for staff', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Staff->delete($id)) {
			$this->Session->setFlash(__('Staff deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Staff was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	function admin_index() {
		$this->Staff->recursive = 0;
		$this->set('staffs', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid staff', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('staff', $this->Staff->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Staff->create();
			if ($this->Staff->save($this->data)) {
				$this->Session->setFlash(__('The staff has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The staff could not be saved. Please, try again.', true));
			}
		}
		$staffTypes = $this->Staff->StaffType->find('list');
		$this->set(compact('staffTypes'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid staff', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Staff->save($this->data)) {
				$this->Session->setFlash(__('The staff has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The staff could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Staff->read(null, $id);
		}
		$staffTypes = $this->Staff->StaffType->find('list');
		$this->set(compact('staffTypes'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for staff', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Staff->delete($id)) {
			$this->Session->setFlash(__('Staff deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Staff was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>